cmake_minimum_required(VERSION 3.10)
project(SimpleDB)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 包含目录
include_directories(include)

# 源文件（排除unit_test目录、test_runner.cpp和main文件）
file(GLOB_RECURSE ALL_SOURCES "src/*.cpp")
file(GLOB_RECURSE UNIT_TESTS "src/unit_test/*.cpp")

# 创建基础源文件列表（不含任何main文件）
set(BASE_SOURCES ${ALL_SOURCES})
list(REMOVE_ITEM BASE_SOURCES ${UNIT_TESTS})
list(REMOVE_ITEM BASE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/test_runner.cpp")
list(REMOVE_ITEM BASE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
list(REMOVE_ITEM BASE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/main_simplified.cpp")

# 可执行文件 - 原始版本
add_executable(simpledb ${BASE_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")

# 可执行文件 - 简化版本
add_executable(simpledb2 ${BASE_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/src/main_simplified.cpp")

# 测试
# 词法分析器测试
enable_testing()

# 基础测试
enable_testing()
add_test(NAME test_basic COMMAND simpledb)

# 词法分析器测试
add_executable(test_lexer "src/unit_test/test_lexer.cpp" "src/compiler/lexer.cpp")
add_test(NAME test_lexer COMMAND test_lexer)

# 解析器测试
add_executable(test_parser "src/unit_test/test_parser.cpp" "src/compiler/lexer.cpp" "src/compiler/parser.cpp")
add_test(NAME test_parser COMMAND test_parser)

# 语义分析器测试
add_executable(test_semantic_analyze "src/unit_test/test_semantic_analyze.cpp" "src/compiler/semantic_analyzer.cpp" "src/compiler/lexer.cpp" "src/compiler/parser.cpp" "src/execution/catalog.cpp" "src/storage/buffer_pool.cpp" "src/storage/disk_manager.cpp" "src/storage/page.cpp")
add_test(NAME test_semantic_analyze COMMAND test_semantic_analyze)

# 计划器测试
add_executable(test_planner "src/unit_test/test_planner.cpp" "src/compiler/planner.cpp" "src/compiler/lexer.cpp" "src/compiler/parser.cpp" "src/execution/catalog.cpp" "src/storage/buffer_pool.cpp" "src/storage/disk_manager.cpp" "src/storage/page.cpp")
add_test(NAME test_planner COMMAND test_planner)

# DiskManager 单独测试
add_executable(test_disk_manager "src/unit_test/test_disk_manager.cpp" "src/storage/disk_manager.cpp")
add_test(NAME test_disk_manager COMMAND test_disk_manager)

# Page 类单独测试
add_executable(test_page "src/unit_test/test_page.cpp" "src/storage/page.cpp")
add_test(NAME test_page COMMAND test_page)

# 表达式求值器测试
add_executable(test_expression_evaluator "src/unit_test/test_expression_evaluator.cpp" "src/storage/disk_manager.cpp" "src/storage/buffer_pool.cpp" "src/storage/page.cpp" "src/execution/catalog.cpp" "src/execution/expression_evaluator.cpp")
add_test(NAME test_expression_evaluator COMMAND test_expression_evaluator)

# 目录管理器测试
add_executable(test_catalog_new
    src/unit_test/test_catalog_new.cpp
    src/execution/catalog.cpp
    src/storage/buffer_pool.cpp
    src/storage/disk_manager.cpp
    src/storage/page.cpp
)
add_test(NAME test_catalog_new COMMAND test_catalog_new)

# BufferPool测试
add_executable(test_buffer_pool "src/unit_test/test_buffer_pool.cpp" "src/storage/buffer_pool.cpp" "src/storage/disk_manager.cpp" "src/storage/page.cpp")
add_test(NAME test_buffer_pool COMMAND test_buffer_pool)

# Add ultimate minimal executor test
execute_process(COMMAND cmake -E echo "Adding test_executor_ultimate target...")
add_executable(test_executor_ultimate
    src/unit_test/test_executor_ultimate.cpp
    src/execution/executor.cpp
    src/execution/catalog.cpp
    src/execution/expression_evaluator.cpp
    src/compiler/lexer.cpp
    src/compiler/parser.cpp
    src/compiler/semantic_analyzer.cpp
    src/compiler/planner.cpp
    src/storage/buffer_pool.cpp
    src/storage/disk_manager.cpp
    src/storage/page.cpp
)
add_test(NAME test_executor_ultimate COMMAND test_executor_ultimate)



# 测试运行器 - 命令行界面
add_executable(test_runner "src/test_runner.cpp")

# 确保test_runner能正确找到标准库头文件
target_include_directories(test_runner PRIVATE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)
